/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseNewsDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseNewsDao : BaseDao
    {
        public BaseNewsDao()
        {
            base.CurrentTableName = BaseNewsTable.TableName;
        }

        public BaseNewsDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseNewsDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseNewsDao(string tableName)
        {
            base.CurrentTableName = tableName;
        }

        public BaseNewsDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public BaseNewsDao(IDbHelper dbHelper, BaseUserInfo userInfo, string tableName)
            : this(dbHelper, userInfo)
        {
            base.CurrentTableName = tableName;
        }

        public string Add(string folderID, string fileName, string file, string description, string category, bool enabled, out string statusCode)
        {
            return this.Add(folderID, fileName, null, file, description, category, enabled, out statusCode);
        }

        private string Add(string folderID, string fileName, string file, string byteFile, string description, string category, bool enabled, out string statusCode)
        {
            statusCode = string.Empty;
            BaseNewsEntity fileEntity = new BaseNewsEntity
            {
                FolderID = folderID,
                FileName = fileName,
                FileContent = byteFile,
                Description = description,
                Category = category,
                Enabled = enabled
            };
            string str = string.Empty;
            if (this.Exists(BaseNewsTable.FieldFolderID, fileEntity.FolderID, BaseNewsTable.FieldFileName, fileEntity.FileName))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return str;
            }
            str = this.AddEntity(fileEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddEntity(BaseNewsEntity fileEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, base.CurrentTableName);
            if (string.IsNullOrEmpty(fileEntity.ID))
            {
                fileEntity.ID = sequence;
            }
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(base.CurrentTableName);
            builder.SetValue(BaseNewsTable.FieldID, fileEntity.ID);
            builder.SetValue(BaseNewsTable.FieldFolderID, fileEntity.FolderID);
            builder.SetValue(BaseNewsTable.FieldFileName, fileEntity.FileName);
            if (fileEntity.FileContent != null)
            {
                builder.SetValue(BaseNewsTable.FieldFileSize, fileEntity.FileContent.Length);
            }
            builder.SetValue(BaseNewsTable.FieldFilePath, fileEntity.FilePath);
            builder.SetValue(BaseNewsTable.FieldCategory, fileEntity.Category);
            builder.SetValue(BaseNewsTable.FieldDescription, fileEntity.Description);
            builder.SetValue(BaseNewsTable.FieldEnabled, fileEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseNewsTable.FieldSortCode, sequence);
            builder.SetValue(BaseNewsTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetValue(BaseNewsTable.FieldCreateUserRealname, base.UserInfo.Realname);
            builder.SetDBNow(BaseNewsTable.FieldCreateDate);
            builder.SetValue(BaseNewsTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetValue(BaseNewsTable.FieldModifyUserRealname, base.UserInfo.Realname);
            builder.SetDBNow(BaseNewsTable.FieldModifyDate);
            builder.EndInsert();
            builder.BeginUpdate(base.CurrentTableName);
            if (fileEntity.FileContent != null)
            {
                builder.SetValue(BaseNewsTable.FieldFileContent, fileEntity.FileContent);
            }
            builder.SetWhere(BaseNewsTable.FieldID, sequence);
            if (builder.EndUpdate() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public override int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BaseNewsEntity fileEntity = new BaseNewsEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseNewsTable.FieldID, DataRowVersion.Original].ToString();
                    if (id.Length > 0)
                    {
                        num += this.Delete(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BaseNewsTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    fileEntity.GetFrom(row);
                    num += this.UpdateEntity(fileEntity);
                }
                if (row.RowState == DataRowState.Added)
                {
                    fileEntity.GetFrom(row);
                    num += (this.AddEntity(fileEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        public int DeleteByFolder(string folderID)
        {
            return this.Delete(BaseNewsTable.FieldFolderID, folderID);
        }

        public string Download(string id)
        {
            this.UpdateReadCount(id);
            string str = null;
            string sqlQuery = " SELECT " + BaseNewsTable.FieldFileContent + "   FROM " + base.CurrentTableName + "  WHERE " + BaseNewsTable.FieldID + " = " + base.DbHelper.GetParameter(BaseNewsTable.FieldID);
            IDataReader reader = null;
            try
            {
                reader = base.DbHelper.ExecuteReader(sqlQuery, base.DbHelper.MakeParameters(BaseNewsTable.FieldID, id));
                if (reader.Read())
                {
                    str = reader[BaseNewsTable.FieldFileContent].ToString();
                }
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(base.DbHelper, base.UserInfo, exception);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return str;
        }

        public BaseNewsEntity Download(string id, BaseNewsEntity fileEntity)
        {
            this.UpdateReadCount(id);
            string sqlQuery = " SELECT " + BaseNewsTable.FieldFileContent + "   FROM " + base.CurrentTableName + "  WHERE " + BaseNewsTable.FieldID + " = " + base.DbHelper.GetParameter(BaseNewsTable.FieldID);
            IDataReader reader = null;
            try
            {
                reader = base.DbHelper.ExecuteReader(sqlQuery, base.DbHelper.MakeParameters(BaseNewsTable.FieldID, id));
                if (reader.Read())
                {
                    fileEntity.FileContent = reader[BaseNewsTable.FieldFileContent].ToString();
                }
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(base.DbHelper, base.UserInfo, exception);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return fileEntity;
        }

        public DataTable GetDTByFolder(string id)
        {
            string sqlQuery = string.Empty;
            sqlQuery = " SELECT " + BaseNewsTable.FieldID + "        ," + BaseNewsTable.FieldFolderID + "        ," + BaseNewsTable.FieldFileName + "        ," + BaseNewsTable.FieldFilePath + "        ," + BaseNewsTable.FieldFileSize + "        ," + BaseNewsTable.FieldReadCount + "        ," + BaseNewsTable.FieldCategory + "        ," + BaseNewsTable.FieldDescription + "        ," + BaseNewsTable.FieldEnabled + "        ," + BaseNewsTable.FieldDeleteMark + "        ," + BaseNewsTable.FieldSortCode + "        ," + BaseNewsTable.FieldCreateUserID + "        ," + BaseNewsTable.FieldCreateUserRealname + "        ," + BaseNewsTable.FieldCreateDate + "        ," + BaseNewsTable.FieldModifyUserID + "        ," + BaseNewsTable.FieldModifyUserRealname + "        ," + BaseNewsTable.FieldModifyDate + "       , (SELECT " + BaseFolderTable.FieldFolderName + " FROM " + BaseFolderTable.TableName + " WHERE " + BaseFolderTable.FieldID + " = " + BaseNewsTable.FieldFolderID + ") AS FolderFullName  FROM " + base.CurrentTableName + " WHERE " + BaseNewsTable.FieldFolderID + " = " + base.DbHelper.GetParameter(BaseNewsTable.FieldFolderID);
            DataTable dataTable = new DataTable(base.CurrentTableName);
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(BaseNewsTable.FieldFolderID, id));
            if (id.Length == 0)
            {
                DataRow row = dataTable.NewRow();
                row[BaseNewsTable.FieldEnabled] = 1;
                dataTable.Rows.Add(row);
                dataTable.AcceptChanges();
            }
            this.GetFrom(dataTable);
            return dataTable;
        }

        public BaseNewsEntity GetEntity(string id)
        {
            string sqlQuery = string.Empty;
            sqlQuery = " SELECT " + BaseNewsTable.FieldID + "        ," + BaseNewsTable.FieldFolderID + "        ," + BaseNewsTable.FieldFileName + "        ," + BaseNewsTable.FieldFilePath + "        ," + BaseNewsTable.FieldFileSize + "        ," + BaseNewsTable.FieldReadCount + "        ," + BaseNewsTable.FieldCategory + "        ," + BaseNewsTable.FieldDescription + "        ," + BaseNewsTable.FieldEnabled + "        ," + BaseNewsTable.FieldDeleteMark + "        ," + BaseNewsTable.FieldSortCode + "        ," + BaseNewsTable.FieldCreateUserID + "        ," + BaseNewsTable.FieldCreateUserRealname + "        ," + BaseNewsTable.FieldCreateDate + "        ," + BaseNewsTable.FieldModifyUserID + "        ," + BaseNewsTable.FieldModifyUserRealname + "        ," + BaseNewsTable.FieldModifyDate + "      ,(SELECT " + BaseFolderTable.FieldFolderName + " FROM " + BaseFolderTable.TableName + " WHERE " + BaseFolderTable.FieldID + " = " + BaseNewsTable.FieldFolderID + ") AS FolderFullName  FROM " + base.CurrentTableName + " WHERE " + BaseNewsTable.FieldID + " = " + base.DbHelper.GetParameter(BaseNewsTable.FieldID);
            DataTable dataTable = new DataTable(base.CurrentTableName);
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(BaseNewsTable.FieldID, id));
            return new BaseNewsEntity(dataTable);
        }

        public int MoveTo(string id, string folderID)
        {
            string property = this.GetProperty(id, BaseNewsTable.FieldFileName);
            this.Delete(BaseNewsTable.FieldFolderID, folderID, BaseNewsTable.FieldFileName, property);
            return this.SetProperty(id, BaseNewsTable.FieldFolderID, folderID);
        }

        public DataTable Search(string search)
        {
            return this.Search(string.Empty, search, false);
        }

        public DataTable Search(string folderID, string search)
        {
            return this.Search(folderID, search, false);
        }

        public DataTable Search(string folderID, string search, bool deleteMark)
        {
            string sqlQuery = string.Empty;
            string str2 = deleteMark ? "1" : "0";
            sqlQuery = " SELECT " + BaseNewsTable.FieldID + "        ," + BaseNewsTable.FieldFolderID + "        ," + BaseNewsTable.FieldFileName + "        ," + BaseNewsTable.FieldFilePath + "        ," + BaseNewsTable.FieldFileSize + "        ," + BaseNewsTable.FieldReadCount + "        ," + BaseNewsTable.FieldDescription + "        ," + BaseNewsTable.FieldCategory + "        ," + BaseNewsTable.FieldEnabled + "        ," + BaseNewsTable.FieldDeleteMark + "        ," + BaseNewsTable.FieldSortCode + "        ," + BaseNewsTable.FieldCreateUserID + "        ," + BaseNewsTable.FieldCreateUserRealname + "        ," + BaseNewsTable.FieldCreateDate + "        ," + BaseNewsTable.FieldModifyUserID + "        ," + BaseNewsTable.FieldModifyUserRealname + "        ," + BaseNewsTable.FieldModifyDate + " FROM " + base.CurrentTableName + " WHERE " + BaseNewsTable.FieldDeleteMark + " = " + str2;
            if (!string.IsNullOrEmpty(folderID))
            {
                string str3 = sqlQuery;
                sqlQuery = str3 + " AND " + BaseNewsTable.FieldFolderID + " = '" + folderID + "'";
            }
            if (!string.IsNullOrEmpty(search))
            {
                string str4 = sqlQuery;
                string str5 = str4 + " AND (" + BaseNewsTable.FieldFileName + " LIKE " + base.DbHelper.GetParameter(BaseNewsTable.FieldFileName);
                string str6 = str5 + " OR " + BaseNewsTable.FieldCreateUserRealname + " LIKE " + base.DbHelper.GetParameter(BaseNewsTable.FieldCreateUserRealname);
                string str7 = str6 + " OR " + BaseNewsTable.FieldFileContent + " LIKE " + base.DbHelper.GetParameter(BaseNewsTable.FieldFileContent);
                sqlQuery = str7 + " OR " + BaseNewsTable.FieldDescription + " LIKE " + base.DbHelper.GetParameter(BaseNewsTable.FieldDescription) + ")";
                search = search.Trim();
                if (search.IndexOf("%") < 0)
                {
                    search = "%" + search + "%";
                }
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseNewsTable.FieldFileName, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseNewsTable.FieldCreateUserRealname, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseNewsTable.FieldFileContent, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseNewsTable.FieldDescription, search));
            }
            DataTable dataTable = new DataTable(base.CurrentTableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public int Update(string id, string folderID, string fileName, string description, bool enabled, out string statusCode)
        {
            statusCode = string.Empty;
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(base.CurrentTableName);
            builder.SetValue(BaseNewsTable.FieldFolderID, folderID);
            builder.SetValue(BaseNewsTable.FieldFileName, fileName);
            builder.SetValue(BaseNewsTable.FieldEnabled, enabled);
            builder.SetValue(BaseNewsTable.FieldDescription, description);
            builder.SetValue(BaseNewsTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetValue(BaseNewsTable.FieldModifyUserRealname, base.UserInfo.Realname);
            builder.SetDBNow(BaseNewsTable.FieldModifyDate);
            builder.SetWhere(BaseNewsTable.FieldID, id);
            int num = builder.EndUpdate();
            if (num > 0)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseNewsEntity fileEntity)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(base.CurrentTableName);
            builder.SetValue(BaseNewsTable.FieldFileName, fileEntity.FileName);
            builder.SetValue(BaseNewsTable.FieldFilePath, fileEntity.FilePath);
            builder.SetValue(BaseNewsTable.FieldDescription, fileEntity.Description);
            builder.SetValue(BaseNewsTable.FieldEnabled, fileEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseNewsTable.FieldSortCode, fileEntity.SortCode);
            builder.SetValue(BaseNewsTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetValue(BaseNewsTable.FieldModifyUserRealname, base.UserInfo.Realname);
            builder.SetDBNow(BaseNewsTable.FieldModifyDate);
            builder.SetWhere(BaseNewsTable.FieldID, fileEntity.ID);
            return builder.EndUpdate();
        }

        public int UpdateFile(string id, string fileName, string file)
        {
            return this.UpdateFile(id, fileName, file, null);
        }

        public int UpdateFile(string id, string fileName, byte[] file)
        {
            return this.UpdateFile(id, fileName, null, file);
        }

        public int UpdateFile(string id, string fileName, byte[] file, out string statusCode)
        {
            statusCode = string.Empty;
            int num = this.UpdateFile(id, fileName, file);
            if (num > 0)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        private int UpdateFile(string id, string fileName, string file, byte[] byteFile)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(base.CurrentTableName);
            builder.SetValue(BaseNewsTable.FieldFileName, fileName);
            if (file != null)
            {
                builder.SetValue(BaseNewsTable.FieldFileContent, file);
            }
            if (byteFile != null)
            {
                builder.SetValue(BaseNewsTable.FieldFileContent, byteFile);
            }
            builder.SetValue(BaseNewsTable.FieldFileSize, file.Length);
            builder.SetValue(BaseNewsTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetValue(BaseNewsTable.FieldModifyUserRealname, base.UserInfo.Realname);
            builder.SetDBNow(BaseNewsTable.FieldModifyDate);
            builder.SetWhere(BaseNewsTable.FieldID, id);
            return builder.EndUpdate();
        }

        private int UpdateReadCount(string id)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(base.CurrentTableName);
            builder.SetFormula(BaseNewsTable.FieldReadCount, BaseNewsTable.FieldReadCount + " + 1 ");
            builder.SetWhere(BaseNewsTable.FieldID, id);
            return builder.EndUpdate();
        }

        public string Upload(string folderID, string fileName, string file)
        {
            string str = this.FindID(BaseNewsTable.FieldFolderID, folderID, BaseNewsTable.FieldFileName, fileName);
            if (!string.IsNullOrEmpty(str))
            {
                this.UpdateFile(str, fileName, file);
                return str;
            }
            BaseNewsEntity fileEntity = new BaseNewsEntity
            {
                FolderID = folderID,
                FileName = fileName,
                FileContent = file
            };
            return this.AddEntity(fileEntity);
        }
    }
}